using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace MetaObjects.Core
{
	public class ActionMapper<TSource, TTarget> : MapperBase<TSource, TTarget>
	{
		public Func<TTarget> Constructor { get; private set; }
		public ReadOnlyCollection<Action<TSource, TTarget>> Actions { get; private set; }

		public ActionMapper(Func<TSource, TTarget> map) : base(map) { }

		public ActionMapper(Func<TTarget> constructor)
			: this(constructor, Enumerable.Empty<Action<TSource, TTarget>>())
		{
		}

		public ActionMapper(Func<TTarget> constructor, IEnumerable<Action<TSource, TTarget>> actions)
		{
			Constructor = constructor;
			Actions = new ReadOnlyCollection<Action<TSource, TTarget>>(actions.ToArray());
		}

		protected override TTarget MapInternal(TSource source)
		{
			TTarget target = Constructor();
			foreach (Action<TSource, TTarget> action in Actions)
			{
				action(source, target);
			}
			return target;
		}
	}
}